import { defineStore } from 'pinia'
import {
  getCategory1API,
  getCategory2API,
  getCategory3API,
} from '@/api/product/attr'
import { ref } from 'vue'

const useCategoruStore = defineStore('category', () => {
  const c1Arr = ref([])
  const c2Arr = ref([])
  const c3Arr = ref([])
  const c1Id = ref()
  const c2Id = ref()
  const c3Id = ref()

  // 一级分类改变时清空二级分类和三级分类的若干数据
  const clearInfoForC1 = () => {
    c2Arr.value = []
    c2Id.value = ''
    c3Arr.value = []
    c3Id.value = ''
  }

  const clearInfoForC2 = () => {
    c3Id.value = ''
    c3Arr.value = []
  }

  const getC1 = async () => {
    let res = await getCategory1API()
    if (res.code === 200) {
      c1Arr.value = res.data
    }
  }

  const getC2 = async () => {
    let res = await getCategory2API(c1Id.value)
    if (res.code === 200) {
      c2Arr.value = res.data
    }
  }

  const getC3 = async () => {
    let res = await getCategory3API(c2Id.value)
    if (res.code === 200) {
      c3Arr.value = res.data
    }
  }

  return {
    c1Arr,
    c2Arr,
    c3Arr,
    c1Id,
    c2Id,
    c3Id,
    getC1,
    getC2,
    getC3,
    clearInfoForC1,
    clearInfoForC2,
  }
})

export default useCategoruStore
